package com.dt76.smallloanbackendservice.service.etao.impl;

import com.dt76.smallloanbackendservice.mapper.etao.MenuRoleMapper;
import com.dt76.smallloanbackendservice.mapper.etao.MeunMapper;
import com.dt76.smallloanbackendservice.pojo.etao.Meun;
import com.dt76.smallloanbackendservice.pojo.etao.RoleMenu;
import com.dt76.smallloanbackendservice.service.etao.MenuService;
import com.dt76.smallloanbackendservice.utils.MenuList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MeunMapper meunMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<Meun> getMeunByEmpId(Integer empId) {
        List<Meun> meuns = meunMapper.selectMeunByEmpId(empId);
        System.out.println(meuns.toString()+"-----------菜单");
        return meuns;
    }

    @Override
    public List<Meun> getMenuList() {
        return meunMapper.getMenuList();
    }

    @Override
    public List<String> getMeunByRId(Integer rId) {
        return meunMapper.selectMeunByRoleId(rId);
    }

    @Override
    public boolean editMenuRole(MenuList menuList) {
        boolean fg = false;
        //首先清除本角色原有权限
        menuRoleMapper.delMenuByRId(menuList.getrId());
        //循环添加新权限
        for (Integer mId:menuList.getCheckMenu()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setmId(mId);
            roleMenu.setrId(menuList.getrId());
//            System.out.println("插入单条角色权限"+mId);
            if (menuRoleMapper.saveMenuRole(roleMenu) == 1) {
                fg = true;
            }
        }
//        System.out.println("修改角色"+fg);
        return fg;
    }

}
